//
//  User.swift
//  ZZBase
//
//  Created by 张文晏 on 2018/9/21.
//  Copyright © 2018 张文晏. All rights reserved.
//  用户

import UIKit
import Moya

/// 用户信息
public struct UserItem: Codable {
    /// 员工 Id
    public var staffId: Int
    /// Tenant id
    public var tenantID: String
    /// 员工姓名
    public var staffName: String
    /// 手机号码
    public var mobile: String
    /// 公司名称
    public var companyName: String
    /// 账号类型（供应商: 0, 客户: 1, master: 2）
    public var clientType: Int
    /// 是否付费（免费: 0, 付费: 1）
    public var paid_type: Int
    /// 是否是 门 企业（否: 0, 是: 1）
    public var industry: Int
    /// 推送频道
    public var channels: [String]
}

/// 系统设置
public struct SystemConfigItem: Decodable {
    /// 权限
    public var authorities: [Int] = []
    /// 自定义订单编号
    public var customOrderNumber: Int = 0
    /// 自定义采购单编号
    public var customPurchaseNumber: Int = 0
    /// 显示产品图片
    public var showImageUrl: Int = 0
    /// 产品数量最大小数位
    public var lengthOfQuantity: Int = 6
    /// 支持多币种
    public var multiCurrency: Int = 0
    /// 启用帮报工
    public var useHelpWorker: Int = 0
    /// 需要录入客户订单号
    public var inputCustomerOrderNumber: Int = 0
    /// 默认开发票
    public var needInvoice: Int = 0
    /// 启用订单审核
    public var verifyFinishedOrder: Int = 0
    /// 启用采购单审核
    public var verifyFinishedPurchase: Int = 0
    /// 开启委外采购流程
    public var enableOutsourcePurchaseFlow: Int = 0
    /// 禁止修改订单产品价格
    public var forbiddenModifyPrice: Int = 0
    /// 录入客户要求日期
    public var inputCustomerRequestDeadLine: Int = 0
    /// 允许修改承诺交期
    public var allowModifyPromiseDeadLine: Int = 0
    /// 销售出库需要扫码
    public var orderInventoryConfirm: Int = 0
    /// 采购入库需要扫码
    public var purchaseReceivingConfirm: Int = 0
    /// 生产入库需要扫码
    public var productionInventoryConfirm: Int = 0
    /// 启用库存批次
    public var useInventoryBatch: Int = 0
    /// 能够采购检验
    public var enableProcurementInspection: Int = 0
    /// 自定义批次编号
    public var customerizedBatchNumber: Int = 0
    /// 使用自己的上传服务器
    public var storageFlag: Int?
    /// 安全库存和最大库存
    public var has_safety_inventory: Int = 0
    
    public init() {}
}

/// 币种信息
public struct CurrencyItem: Decodable {
    /// 币种 id
    public var id: Int = 1
    /// 币种名称(人民币)
    public var desc: String = "人民币"
    /// 币种缩写(CNY)
    public var engDesc: String = "CNY"
    /// 币种符号(¥)
    public var sign: String = "¥"
    /// 对人民币汇率
    public var rate: Decimal = 1
    
    public init() {}
    
    /// 为了适配 OC 代码的数据结构(重构完可删除此方法)
    public func dictValue() -> NSDictionary {
        let dic = NSDictionary(dictionary: [
            "id": id,
            "desc": desc,
            "engDesc": engDesc,
            "sign": sign,
            "rate": rate
            ])
        return dic
    }
}

// MARK: -
/// 用户
open class User {
    
    // MARK: - 单例
    public static let shared = User()
    private init() {}
    
    // MARK: - Variables
    /// 用户信息
    open var info: UserItem?
    /// 系统设置
    open var systemConfig = SystemConfigItem()
    /// 币种信息
    open var currencies = [CurrencyItem()]
    
    
    // MARK: - Actions
    /// 登录
    open func login(username: String, password: String, success: @escaping (_ data: UserItem) -> Void, failure: @escaping (_ error: RequestError) -> Void) {
        baseApiProvider.request(
            BaseApi.login(username: username, password: password),
            model: UserItem.self,
            success: { data in
                self.info = data
                success(data)
        }) { error in
            failure(error)
        }
    }
    
    /// 退出登录
    open func logout(success: @escaping (_ data: BaseResponseData) -> Void, failre: @escaping (_ error: RequestError) -> Void) {
        baseApiProvider.request(
            BaseApi.logout,
            decodeFromData: false,
            model: BaseResponseData.self,
            success: { data in
                self.info = nil
                success(data)
        }) { error in
            failre(error)
        }
    }
    
    /// 获取系统设置
    open func getSystemConfig(success: @escaping (_ data: SystemConfigItem) -> Void, failure: @escaping (_ error: RequestError) -> Void) {
        baseApiProvider.request(
            BaseApi.getSystemConfig,
            model: SystemConfigItem.self,
            success: { data in
                self.systemConfig = data
                success(data)
        }) { error in
            failure(error)
        }
    }
    
    /// 获取币种信息
    open func getCurrencies(success: @escaping (_ data: [CurrencyItem]) -> Void, failure: @escaping (_ error: RequestError) -> Void) {
        baseApiProvider.request(
            BaseApi.getSupportCurrencies,
            model: [CurrencyItem].self,
            success: { data in
                self.currencies = data
                success(data)
        }) { error in
            failure(error)
        }
    }
}
